import './js/weapp-adapter.js'
const ctx = canvas.getContext('2d');

//导入背景绘制模块
import Background from './js/Background.js';
const bg = new Background(ctx);

//导入手推车模块
import Handcart from './js/Handcart.js';
var handcart = new Handcart(ctx);
handcart.listen();

//导入批量绘制模块
 import Pool from './js/Pool.js';
 var pool = new Pool(ctx,handcart);


//导入绘制分数模块
import Info from './js/Info.js';
var info = new Info(ctx);

function gameStart(){
  //清空画布
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  //绘制背景
  bg.draw();
  //绘制手推车
  handcart.draw();
  //批量绘制水果
  pool.drawFriuts(bg.top);
  //绘制炸弹
  pool.drawEnemys(bg.top);
  //绘制分数
  info.showScore(pool.score);
  
  //启动动画
  let stop =window.requestAnimationFrame(gameStart);
  if (handcart.isOver) {
    //终止动画
    window.cancelAnimationFrame(stop);
    info.showGameOver(pool.score);
  }
}
gameStart();

function gameAgain(){
  ctx.canvas.addEventListener('touchstart', (e) => {
    //获取按下时手指的坐标
    // cancelAnimationFrame(gameAgain);
    let dot = e.changedTouches[0];
    if (dot.clientX > info.btnArea.startX && dot.clientX < info.btnArea.startX + info.btnArea.endX && dot.clientY > info.btnArea.startY && dot.clientY < info.btnArea.startY + info.btnArea.endY && handcart.isOver) {
      // requestAnimationFrame(gameAgain);
      wx.showModal({
        title: '确定重新开始游戏吗',
        content: '',
        confirmText:'是',
        cancelText:'否',
        success:res =>{
          if (res.confirm) {
            let top = window.requestAnimationFrame(gameStart);
            window.cancelAnimationFrame(top);
            handcart = new Handcart(ctx);
            pool = new Pool(ctx,handcart);
            handcart.listen();
            gameStart();
          } else if (res.cancel) {
            
            wx.showToast({
              title: '游戏结束',
            })
          }
        }
      })
    }
  });
  ctx.canvas.addEventListener('touchend', (e) => {
    //手指离开屏幕时，重置状态
  });
}
gameAgain();